//
//  RegisterViewController.m
//  IMDome
//
//  Created by limaohuyu on 2020/8/13.
//  Copyright © 2020 czw. All rights reserved.
//

#import "RegisterViewController.h"
#import "BaseServiceManager.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwTextField;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeTextField;
@property (weak, nonatomic) IBOutlet  UIButton *verifyCodeBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
    [self verifyCodeBtnAction:nil];
}

- (IBAction)sureAction:(id)sender {
    
    if (kStringIsEmpty(self.nameTextField.text)) {
        [AnimationView showString:@"账号不能为空"];
        return;
    }
    if (kStringIsEmpty(self.pwTextField.text)) {
        [AnimationView showString:@"密码不能为空"];
        return;
    }
    if (kStringIsEmpty(self.verifyCodeTextField.text)) {
        [AnimationView showString:@"验证码不能为空"];
        return;
    }
    NSMutableDictionary *params  = [NSMutableDictionary dictionary];
    [params setObject:self.nameTextField.text forKey:@"account"];
    [params setObject:self.pwTextField.text forKey:@"password"];
    [params setObject:self.verifyCodeTextField.text forKey:@"code"];

    [BaseServiceManager postRequest:@"user/register" params:params startBlock:^{
        [self showRequestHUDView];
    } successBlock:^(ResponseBaseModel * _Nonnull responseModel) {
        [self hideRequestHUDView];
        [self.navigationController popViewControllerAnimated:YES];
    } failureBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
        [self hideRequestHUDView];

    }];
    
    
}
- (IBAction)verifyCodeBtnAction:(id)sender {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *imgUrl = [NSString stringWithFormat:@"%@user/getImageCode",BASE_URL];
        NSData *imgData=    [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.verifyCodeBtn setImage:[UIImage imageWithData:imgData] forState:UIControlStateNormal];
        });
    });

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
